package com.example.cadmin.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cadmin.entity.ArchivedType;
import com.example.cadmin.mapper.ArchivedTypeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cadmin.utils.DateFormat;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 归档类型 服务实现类
 * </p>
 *
 * @author  zzsoft 
 * @since 2023-02-15
 */
@Service
public class ArchivedTypeService extends ServiceImpl<ArchivedTypeMapper, ArchivedType> {

    @Resource
    public ArchivedTypeMapper archivedTypeMapper;
    /**
     * 归档类型列表
     */
    public Map<String, Object> archivedTypeList(String archivedName, int page, int limit) {
        //参数一是当前页，参数二是每页个数
        IPage<Map<String, Object>> attributePage = new Page<>(page, limit);
        QueryWrapper<ArchivedType> wrapper = new QueryWrapper<>();
        //判断一下是否有搜索条件
        if (StrUtil.isNotEmpty(archivedName)) {
            wrapper.like("archived_name", archivedName);

        }
        attributePage = archivedTypeMapper.selectMapsPage(attributePage, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", attributePage.getTotal());
        map.put("data", attributePage.getRecords());
        return map;
    }

    /**
     * 保存归档类型
     */
    public ResponseModel archivedSave(JSONObject data) {
        QueryWrapper<ArchivedType> queryWrapper = new QueryWrapper<ArchivedType>().eq("archived_name",data.getStr("archivedName"));
        ArchivedType archivedTypeExists = archivedTypeMapper.selectOne(queryWrapper);
        if (archivedTypeExists != null) {
            return ResponseModel.fail("归档类型名称已重复");
        }
        ArchivedType archivedType = new ArchivedType();
        archivedType.setArchivedName(data.getStr("archivedName"));
        archivedType.setCreateTime(DateFormat.getTime());
        archivedType.setUpdateTime(DateFormat.getTime());
        int insertStatus = archivedTypeMapper.insert(archivedType);
        return insertStatus > 0 ? ResponseModel.success("保存归档类型成功") : ResponseModel.fail("保存归档类型失败");
    }

    /**
     * 修改归档类型
     */
    public ResponseModel editInfo(@RequestBody JSONObject data){
        QueryWrapper<ArchivedType> queryWrapper = new QueryWrapper<ArchivedType>().eq("archived_name",data.getStr("archivedName"));
        ArchivedType archivedTypeExists = archivedTypeMapper.selectOne(queryWrapper);
        if (archivedTypeExists != null) {
            return ResponseModel.fail("归档类型名称已重复");
        }
        UpdateWrapper<ArchivedType> updateWrapper = new UpdateWrapper<ArchivedType>().eq("id", data.getInt("archivedId"))
                .set("archived_name", data.getStr("archivedName")).set("update_time", DateFormat.getTime());
        int updateStatus = archivedTypeMapper.update(null, updateWrapper);
        return updateStatus > 0 ? ResponseModel.success("修改归档类型成功") : ResponseModel.fail("修改归档类型失败");
    }

    /**
     * 删除归档类型
     */
    public ResponseModel remove(String archivedId) {
        int delStatus = archivedTypeMapper.deleteById(archivedId);
        return delStatus > 0 ? ResponseModel.success("归档类型删除成功") : ResponseModel.fail("归档类型删除失败");
    }

}
